package com.cd.javacxy.module.web.controller;

import com.cd.javacxy.common.model.entity.CxyBlog;
import com.cd.javacxy.common.model.entity.CxyBlogCategory;
import com.cd.javacxy.common.model.enums.BlogStatusEnum;
import com.cd.javacxy.common.model.query.BlogQuery;
import com.cd.javacxy.common.model.query.UserFriendQuery;
import com.cd.javacxy.common.model.vo.CxyUserVo;
import com.cd.javacxy.common.utils.Constants;
import com.cd.javacxy.common.utils.PageResult;
import com.cd.javacxy.module.base.BaseController;
import com.cd.javacxy.module.web.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * CxyBlog的路由接口服务
 *
 * @author yanghy
 */
@Controller
@Slf4j
public class CxyBlogController extends BaseController {

    /**
     * CxyBlogService服务
     */
    @Autowired
    private CxyBlogService cxyBlogService;

    @Autowired
    private CxyUserService userService;

    @Autowired
    private CxyUserFriendService cxyUserFriendService;
    @Autowired
    private CxyBlogCategoryService cxyBlogCategoryService;

    @GetMapping(value = "/blog/detail/{blogId}")
    public ModelAndView blogDetail(@PathVariable Integer blogId){
        ModelAndView view = new ModelAndView("user/blog_detail");
        try {
            CxyBlog blog = this.cxyBlogService.showBlog(blogId);
            view.addObject("topic", blog);
        } catch (Exception e) {
            log.error("获取博客信息失败：", e);
            view.setViewName("redirect:" + Constants.ERROR_404);
            return view;
        }
        return view;
    }


    @GetMapping(value = "/blog/{userId}")
    public ModelAndView blog(HttpSession session, @PathVariable Integer userId, BlogQuery blogQuery){
        ModelAndView view = new ModelAndView("user/blog");
        try {
            CxyUserVo user = this.userService.findUserInfo4UserHome(userId);
            UserFriendQuery userFriendQuery = new UserFriendQuery();
            userFriendQuery.setUserId(this.getUserId(session));
            userFriendQuery.setFriendUserId(userId);
            view.addObject("focusType", this.cxyUserFriendService.findFocusType4UserHome(userFriendQuery));
            blogQuery.setStatus(BlogStatusEnum.PUBLIC);
            List<CxyBlogCategory> categoryList = this.cxyBlogCategoryService.findBlogCategoryList(userId);
            PageResult<CxyBlog> result = this.cxyBlogService.findBlogByPage(blogQuery);
            view.addObject("user", user);
            view.addObject("categoryList", categoryList);
            view.addObject("result", result);

            //获取粉丝和关注数量
            userFriendQuery = new UserFriendQuery();
            userFriendQuery.setFriendUserId(userId);
            view.addObject("fansCount", this.cxyUserFriendService.findCount(userFriendQuery));
            userFriendQuery = new UserFriendQuery();
            userFriendQuery.setUserId(userId);
            view.addObject("focusCount", this.cxyUserFriendService.findCount(userFriendQuery));
        } catch (Exception e) {
            log.error("获取博客信息失败：", e);
            view.setViewName("redirect:" + Constants.ERROR_404);
            return view;
        }
        return view;
    }
}
